//
//  ViewController.m
//  gaodeMap
//
//  Created by dllo on 16/4/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import <AMapNaviKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <AMapSearchKit/AMapSearchObj.h>
#import "NavigationViewController.h"//路线的VC;
#import "ChatViewController.h"
#import "model.h"

@interface ViewController ()<MAMapViewDelegate,AMapSearchDelegate,UITableViewDataSource,UITableViewDelegate,CLLocationManagerDelegate>
@property(nonatomic,strong)MAMapView *mapView;
@property(nonatomic,strong)UIButton *Locaton;//定位按钮
@property(nonatomic,strong)UIButton *tracking;//跟踪后按钮
@property(nonatomic,strong)MAPointAnnotation *pointAnnotation;//大头针
@property(nonatomic,strong)UIButton *searchButton;//搜索功能;
@property(nonatomic,strong)AMapSearchAPI *search;
@property(nonatomic,assign)CGFloat latitude;
@property(nonatomic,assign)CGFloat longitude;
@property(nonatomic,strong)UILabel *lable;
@property(nonatomic,strong)UITextField *searchField;
@property(nonatomic,strong)UITableView *TipsSearch;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,copy)NSString *rowCity;
@property(nonatomic,strong)UITextField *textField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [MAMapServices sharedServices].apiKey =@"be1925d505bdc43b003a0d9909f29a5b";
    _mapView = [[MAMapView alloc]initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, self.view.frame.size.height-200)];
    self.mapView.delegate= self;
    [self.view addSubview:_mapView];
    
    self.Locaton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.Locaton.frame =CGRectMake(5, 10, 50, 30);
    self.Locaton.backgroundColor =[UIColor redColor];
    [self.Locaton setTitle:@"定位" forState:UIControlStateNormal];
    [self.Locaton addTarget:self action:@selector(Locaton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.Locaton];
    
    self.searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.searchButton.frame =CGRectMake(65, 10, 50, 30);
    self.searchButton.backgroundColor =[UIColor redColor];
    [self.searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    [self.searchButton addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.searchButton];
    
    self.lable=[[UILabel alloc]initWithFrame:CGRectMake(0, 45, 400, 30)];
    self.lable.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.lable];
    
    self.searchField=[[UITextField alloc]initWithFrame:CGRectMake(300, 10, 100, 30)];
    self.searchField.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.searchField];
    
    UIButton*sear = [UIButton buttonWithType:UIButtonTypeCustom];
    sear.frame = CGRectMake(0, 80, 50, 30);
    sear.backgroundColor = [UIColor redColor];
    [sear addTarget:self action:@selector(searchField:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sear];
    
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(150, 10, 100, 30)];
    self.textField.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.textField];
    
    
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    searchBtn.frame = CGRectMake(100, 80, 100, 30);
    searchBtn.backgroundColor = [UIColor redColor];
    [searchBtn setTitle:@"POI" forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:searchBtn];
    
    UIButton *near = [UIButton buttonWithType:UIButtonTypeCustom];
    [near setTitle:@"附近" forState:UIControlStateNormal];
    near.backgroundColor = [UIColor blueColor];
    near.frame = CGRectMake(0, self.view.frame.size.height-50, 80, 30);
    [near addTarget:self action:@selector(nearBut:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:near];
    
    UIButton *LoadLine = [UIButton buttonWithType:UIButtonTypeCustom];
    [LoadLine setTitle:@"路线" forState:UIControlStateNormal];
    LoadLine.backgroundColor = [UIColor blueColor];
    LoadLine.frame = CGRectMake(100, self.view.frame.size.height-50, 80, 30);
    [LoadLine addTarget:self action:@selector(LoadLineBut:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:LoadLine];
    
    UIButton *chatButton = [UIButton buttonWithType:UIButtonTypeSystem];
    chatButton.frame = CGRectMake(210, 80, 50, 30);
    [chatButton setTitle:@"环信" forState:UIControlStateNormal];
    [chatButton addTarget:self action:@selector(chat) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:chatButton];
    
}

#pragma mark 环信的页面
-(void)chat{
    ChatViewController *chat = [[ChatViewController alloc]init];
    
    [self presentViewController:chat animated:YES completion:nil];
}

#pragma mark 附近
-(void)nearBut:(UIButton*)near{

}


-(void)LoadLineBut:(UIButton*)but{
    NavigationViewController *na = [[NavigationViewController alloc]init];
    [self presentViewController:na animated:YES completion:^{
        
    }];
}

-(void)searchAction
{
    //配置用户Key
    [AMapSearchServices sharedServices].apiKey = @"be1925d505bdc43b003a0d9909f29a5b";
    
    //初始化检索对象
    _search = [[AMapSearchAPI alloc] init];
    _search.delegate = self;
    
    //构造AMapPOIAroundSearchRequest对象，设置周边请求参数
    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
    request.location = [AMapGeoPoint locationWithLatitude:39.990459 longitude:116.481476];
    request.keywords = @"大连";
    request.types = @"餐饮服务";
    request.sortrule = 0;
    request.requireExtension = YES;
    
    //发起周边搜索
    [_search AMapPOIAroundSearch: request];
}

//实现POI搜索对应的回调函数
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    if(response.pois.count == 0)
    {
        return;
    }
    
    //通过 AMapPOISearchResponse 对象处理搜索结果
    NSString *strCount = [NSString stringWithFormat:@"count: %ld",response.count];
    NSString *strSuggestion = [NSString stringWithFormat:@"Suggestion: %@", response.suggestion];
    NSString *strPoi = @"";
    for (AMapPOI *p in response.pois) {
        strPoi = [NSString stringWithFormat:@"%@\nPOI: %@", strPoi, p.description];
    }
    NSString *result = [NSString stringWithFormat:@"%@ \n %@ \n %@", strCount, strSuggestion, strPoi];
    NSLog(@"Place: %@", result);
}




#pragma mark 开始定位模式
-(void)Locaton:(UIButton*)but{
    self.mapView.showsUserLocation = YES;
    //用户跟踪模式
    [self.mapView setUserTrackingMode:MAUserTrackingModeFollowWithHeading animated:YES];
}


#pragma mark 当位置更新是,会进定位回调,通过回调函数,能获取到定位点的经纬度
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    if (updatingLocation) {
        //取出当前位置的坐标
        NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
        self.latitude =userLocation.coordinate.latitude;
        self.longitude =userLocation.coordinate.longitude;
        [self.searchButton addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    
}

#pragma mark 开始搜索
-(void)search:(UIButton*)but{
    [AMapSearchServices sharedServices].apiKey = @"be1925d505bdc43b003a0d9909f29a5b";
    self.search = [[AMapSearchAPI alloc]init];
    self.search.delegate =self;
    //构造AMapReGeocodeSearchRequest对象
    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
    regeo.location = [AMapGeoPoint locationWithLatitude:self.latitude longitude:self.longitude];
    //查询的范围
    regeo.radius = 1000000;
    regeo.requireExtension = YES;
    [_search AMapReGoecodeSearch:regeo];
}

//实现逆地理编码的回调函数
-(void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response{
    if (response.regeocode !=nil) {
        //通过AMapReGeocodeSearchResponse对象处理搜索结果
        NSString *result = [NSString stringWithFormat:@" %@", response.regeocode.addressComponent.city];
        NSLog(@"=======city:%@",result);
        self.rowCity = result;
        self.lable.text =result;
        NSLog(@"%@",self.lable.text);
    }
    
}
#pragma mark 正向地理的
-(void)searchField:(UIButton*)text{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame =CGRectMake(0, 30, 20, 20);
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(hinde:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    self.TipsSearch = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height)];
    self.TipsSearch.alpha = 0.8;
    self.TipsSearch.delegate = self;
    self.TipsSearch.dataSource =self;
    [self.TipsSearch registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self.view addSubview:self.TipsSearch];
    
    [AMapSearchServices sharedServices].apiKey = @"be1925d505bdc43b003a0d9909f29a5b";
    self.search = [[AMapSearchAPI alloc]init];
    self.search.delegate = self;
    //构造AMapInputTipsSearchRequest对象,设置请求参数
    AMapInputTipsSearchRequest *request = [[AMapInputTipsSearchRequest alloc]init];
    request.keywords = self.searchField.text;
    request.city = self.textField.text;
    NSLog(@" request.city=========%@",request.city);
    //发起输入提示搜索
    [self.search AMapInputTipsSearch:request];
}

-(void)hinde:(UIButton*)but{
    but.hidden = YES;
    self.TipsSearch.hidden = YES;
    
}

//实现输入提示的回调函数
-(void)onInputTipsSearchDone:(AMapInputTipsSearchRequest *)request response:(AMapInputTipsSearchResponse *)response{
    if (response.tips.count == 0) {
        return;
    }
    //通过AMapInputTipsSearchResponse对象处理搜索结果
    NSString *strcount = [NSString stringWithFormat:@"count :%ld",response.count];
    NSString *strTips= @"";
    self.dataArr = [NSMutableArray array];
    for (AMapTip *p in response.tips) {
        [self.dataArr addObject:p];
        [self.TipsSearch reloadData];
        strTips = [NSString stringWithFormat:@"%@\nTip:%@",strTips,p.description];
        NSLog(@"%@",p.name);
    }
    
    NSString *Result = [NSString stringWithFormat:@"%@\n%@",strcount,strTips];
    NSLog(@"InputTips: %@", Result);
    
    
    
}




-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.searchField resignFirstResponder];
    [self.textField resignFirstResponder];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    model *d = [[model alloc]init];
    d = self.dataArr[indexPath.row];
    cell.textLabel.text = d.name;
    return cell;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
